package cn.tedu._02notice.controller;

import cn.tedu._02notice.base.response.JsonResult;
import cn.tedu._02notice.base.response.StatusCode;
import cn.tedu._02notice.mapper.NoticeMapper;
import cn.tedu._02notice.pojo.dto.NoticeAddParam;
import cn.tedu._02notice.pojo.dto.NoticeAddQuery;
import cn.tedu._02notice.pojo.entity.Notice;
import cn.tedu._02notice.pojo.vo.NoticeAddVo;
import cn.tedu._02notice.pojo.vo.NoticeDetailInfoVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
@Tag(name = "咨询管理")
@Slf4j
@RestController
@RequestMapping("/v1/notice/")
public class NoticeController {
    //添加功能
   @Autowired
   private NoticeMapper noticeMapper;
    @PostMapping("add")
    @Operation(summary = "添加咨询")
    @ApiOperationSupport(order = 10)
    @Parameter(name = "noticeAddParam", description = "添加咨询参数", required = true,hidden=true)
    public JsonResult insert(NoticeAddParam noticeAddParam){
        log.debug(String.valueOf(noticeAddParam));
        //实例化实体类
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddParam,notice);
        notice.setUser_id(1L);
        notice.setCreate_time(new Date());
        notice.setUpdate_time(new Date());
        // 调用mapper接口插入数据
        int num = noticeMapper.insert(notice);
        if (num>0){
            return  JsonResult.ok();
        }else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }
    @GetMapping("list")
    @Operation(summary = "查询咨询")
    @ApiOperationSupport(order = 20)
    @Parameter(name = "noticeAddQuery", description = "查询咨询参数", required = true)
    public JsonResult select(NoticeAddQuery noticeAddQuery){
        List<NoticeAddVo> list =noticeMapper.select(noticeAddQuery);
        return JsonResult.ok(list);
    }
    @GetMapping("detail")
    @Operation(summary = "查询咨询详情")
    @ApiOperationSupport(order = 30)
    @Parameter(name ="id", description = "咨询id", required = true,  example = "1")
    public JsonResult detail( Long id){
        NoticeDetailInfoVO noticeDetailInfoVO = noticeMapper.detail(id);
        return new JsonResult(StatusCode.OPERATION_SUCCESS, noticeDetailInfoVO);
    }
}
